import { getData } from '../../api/productApi'
import ProductItem from '@/model/productItem'
export default  class ProductDataAction{
    products: Array<ProductItem>=[];
    
     constructor(products?:Array<ProductItem>) {
        //this.products = products;
        //this.loadProducts();
     }
    //初始化数据
   public async loadProducts():Promise<void> {
       this.products = await getData();
       console.log("load-----")
       console.log(this.products);
       
   }
    
    public getProducts() : Array<ProductItem> {
        return this.products
    }
    //库存-1
    decrementProductInventory( id:number) {   
        const product = this.products.find((product: ProductItem) => product.id === id);
        //存在
        if (product) {
            console.log(product,"product");
            product.inventory--;
        }    
    }
    
}

